package array;

public class test {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n + 1];

        // 初始化dp数组
        dp[0] = 0;
        dp[1] = 0;

        // 动态规划求解最小花费
        for (int i = 2; i <= n; i++) {
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }

        // 返回最终结果，选择最后一个或倒数第二个元素中的较小值
        return Math.min(dp[n], dp[n - 1]);
    }

    public static void main(String[] args) {
        test solution = new test();

        int[] cost1 = {10, 15, 20};
        System.out.println("最低花费：" + solution.minCostClimbingStairs(cost1)); // 输出 15

        int[] cost2 = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};
        System.out.println("最低花费：" + solution.minCostClimbingStairs(cost2)); // 输出 6
    }
}
